<%@ page import="java.awt.*" %>
<%@ page import="java.util.Random" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="java.awt.image.BufferedImage" %><%--
  Created by IntelliJ IDEA.
  Users: hzy
  Date: 2020/1/4
  Time: 13:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="image/jpeg;charset=UTF-8" language="java" %>
<%!
   public Color getColor(){
       Random ran=new Random();
       int r=ran.nextInt(256);
       int g=ran.nextInt(256);
       int b=ran.nextInt(256);

       return new Color(r,g,b);
   }
   public String getName(){
       int a=(int)(Math.random()*25)+65;
       int b=(int)(Math.random()*25)+65;
       int c=(int)(Math.random()*25)+65;
       int d=(int)(Math.random()*25)+65;
       return ""+(char)a+(char)b+(char)c+(char)d;
   }
%>
<%
    response.setHeader("pragma","no-cache");
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("Expires","0");
    BufferedImage bf=new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
    Graphics gh=bf.getGraphics();
    gh.fillRect(0,0,100,30);
    gh.setFont(new Font("seif",Font.BOLD,20));
    gh.setColor(Color.black);
    String checkCode=getName();
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<checkCode.length();i++){
        sb.append(checkCode.charAt(i)+" ");
    }
    gh.drawString(sb.toString(),12,25);
    for(int i=0;i<60;i++){
        Random ran=new Random();
        int xBegin = ran.nextInt(80);
        int yBegin = ran.nextInt(30);
        int xEnd=ran.nextInt(xBegin+10);
        int yEnd = ran.nextInt(yBegin + 10);
        gh.setColor(getColor());
        gh.drawLine(xBegin,yBegin,xEnd,yEnd);
    }
    session.removeAttribute("CHECKCODE");
    session.setAttribute("CHECKCODE",checkCode);
    ImageIO.write(bf,"jpeg",response.getOutputStream());
    out.clear();
    out=pageContext.pushBody();
%>
